package com.hcb.micro.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.ProductSummary;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductService;
import com.hcb.micro.service.IProductSummaryService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping("productSummary")
public class ProductSummaryController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IProductService productService;
	
	@Autowired
	IProductSummaryService productSummaryService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() throws FileNotFoundException, IOException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		List<ProductSummary> productSummaryList = new ArrayList<>(),productSummaryListAll = new ArrayList<>();
		Map<String,Object> para = new HashMap<>();
		
		if (bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			para.put("productId", bodyInfo.getString("productId"));
		}
		int count = 0;
			
		productSummaryListAll = productSummaryService.selectByPara(para);
		count = productSummaryListAll.size();
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		productSummaryList = productSummaryService.selectByPara(para);
		if(productSummaryList.size() <= 0){
			json.put("result", "1");
			json.put("description", "商品规格列表不存在");
			return buildReqJsonObject(json);
		}
		
		List<Map<String,Object>> list = new ArrayList<>();
		for(ProductSummary ps:productSummaryList){
			Map<String,Object> map = new HashMap<>();
			
			map.put("id", ps.getId());
			map.put("productId", ps.getProductId());
			map.put("summaryName", ps.getSummaryName());
			map.put("summaryDesc", ps.getSummaryDesc());
			
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取商品规格列表成功");
		json.put("list", list);
		json.put("count", count);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() throws FileNotFoundException, IOException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		int right = 1;
		if(bodyInfo.get("productSummaryId") == null || "".equals(bodyInfo.get("productSummaryId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		ProductSummary ps = productSummaryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSummaryId")));
		if(ps == null){
			json.put("result", "1");
			json.put("description", "商品规格不存在");
			return buildReqJsonObject(json);
		}
		Map<String,Object> map = new HashMap<>();
		
		map.put("id", ps.getId());
		map.put("productId", ps.getProductId());
		map.put("summaryName", ps.getSummaryName());
		map.put("summaryDesc", ps.getSummaryDesc());
        
		json.put("result", "0");
		json.put("description", "获取商品规格信息成功");
		json.put("productSummary", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productSummaryId") == null || "".equals(bodyInfo.get("productSummaryId"))) {
			right = 0;
		}else if(bodyInfo.get("productId") == null || "".equals(bodyInfo.get("productId"))) {
			right = 0;
		}else if(bodyInfo.get("summaryName") == null || "".equals(bodyInfo.get("summaryName"))){
			right = 0;
		}else if(bodyInfo.get("summaryDesc") == null || "".equals(bodyInfo.get("summaryDesc"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSummary ps = productSummaryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSummaryId")));
		if(ps == null){
			json.put("result", "1");
			json.put("description", "商品规格不存在");
			return buildReqJsonObject(json);
		}
		
		ps.setSummaryName(bodyInfo.getString("summaryName"));
		ps.setSummaryDesc(bodyInfo.getString("summaryDesc"));

		Integer updatePS = productSummaryService.updateByPrimaryKeySelective(ps);
		if(updatePS == 0){
			json.put("result", "1");
			json.put("description", "更新商品规格出错");
			return buildReqJsonObject(json);
		}
		
		json.put("result", "0");
		json.put("description", "修改商品规格成功");
		return buildReqJsonObject(json);
	}
	
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productId") == null || "".equals(bodyInfo.get("productId"))) {
			right = 0;
		}else if(bodyInfo.get("summaryName") == null || "".equals(bodyInfo.get("summaryName"))){
			right = 0;
		}else if(bodyInfo.get("summaryDesc") == null || "".equals(bodyInfo.get("summaryDesc"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSummary ps = new ProductSummary();
		
		ps.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
		ps.setSummaryName(bodyInfo.getString("summaryName"));
		ps.setSummaryDesc(bodyInfo.getString("summaryDesc"));

		Integer updatePS = productSummaryService.insertSelective(ps);
		if(updatePS == 0){
			json.put("result", "1");
			json.put("description", "创建商品规格出错");
			return buildReqJsonObject(json);
		}
		
		json.put("result", "0");
		json.put("description", "创建商品规格成功");
		json.put("productSummaryId", ps.getId());
		return buildReqJsonObject(json);
	}
	
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		int right = 1;
		if(bodyInfo.get("productSummaryId") == null || "".equals(bodyInfo.get("productSummaryId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		ProductSummary ps = productSummaryService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("productSummaryId")));
		if(ps == null){
			json.put("result", "1");
			json.put("description", "商品规格不存在");
			return buildReqJsonObject(json);
		}
		
		Integer rs = productSummaryService.deleteByPrimaryKey(ps.getId());

		if(rs != 1){
			json.put("result", "1");
			json.put("description", "商品规格删除失败");
		}else{
			json.put("result", "0");
			json.put("description", "商品规格删除成功");
		}
		return buildReqJsonObject(json);
	}
}
